<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/28
 * Time: 15:54
 */

namespace Api\Controller;


use Think\Controller;

class OrdersController extends Controller
{
    //@todo  添加订单
    public function addOrderInfo(){
        $post=json_decode(file_get_contents("php://input"),true);
        if($post['pay_type']==1){
            $status=1;
        }else{
            $status=0;
        }
        $data=array(
            'orders_no'=>'WX'.time(),
            'user_id'=>$post['user_id'],
            'xk_id'=>$post['xk_id'],
            'xc_id'=>$post['xc_id'],
            'mark'=>$post['mark'],
            'pay_type'=>$post['pay_type'],
            'ac_time'=>time(),
            'price'=>$post['price'],
            'status'=>$status
        );
        //先添加订单编号
        $id=$this->addOrderNO($data);
        if($id){
            //再 添加
            $add_goods=$this->addGoodsList($id,$post['user_id']);
            if($add_goods){
                //发送邮件
                if($post['pay_type']==1){
                    $this->send_Mail($id);
                }
                $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>$id));
            }
            $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'服务器发生错误')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'服务器发生错误')));
    }

    //@todo 添加订单编号
    public function addOrderNO($data){
        $id=D('orders_list')->add($data);
        return $id;
    }

    //@todo 添加订单商品信息
    public function addGoodsList($id,$user_id){
        //先查询图片中心的数据
        $photo_list=D('create_order')->where(array('user_id'=>$user_id))->select();
        $data=array();
        foreach($photo_list as $key=>$value){
            $data[$key]['orders_id']=$id;
            $data[$key]['product_id']=$value['product_id'];
            $data[$key]['decorate_id']=$value['decorate_id'];
            $data[$key]['photo_url']=$value['photo'];
        }
        $add_all=D('orders_goods_list')->addAll($data);
        if($add_all){
            $del=D('create_order')->where(array('user_id'=>$user_id))->delete();
            return true;
        }else{
            return false;
        }

    }

    //@todo 发送邮件处理
    public function send_Mail($id){
        //根据ID查询订单信息
        $orderInfo=D('orders_list')->find($id);
        $content='温馨提示:收到新的订单啦！订单编号为:['.$orderInfo['orders_no'].'],
        订单时间为:['.date("Y-m-d H:i:s").'],订单金额为:['.$orderInfo['price'].']!请尽快处理！此邮件为通知邮件，勿回复！';
        $mailList=C('Send_User');
        foreach($mailList as $key =>$value){
            sendMail($value,'您有新订单啦',$content);
        }
    }


    //@todo 获取订单状态数
    public function getOrdersList(){
        $post=json_decode(file_get_contents("php://input"),true);
        $user_id=$post['user_id'];
        //未付款条数
        $none_pay=D('orders_list')->where(array('user_id'=>$user_id,'status'=>0))->count();

        //已付款
        $can_pay=D('orders_list')->where(array('user_id'=>$user_id,'status'=>1))->count();

        //已发货
        $can_get=D('orders_list')->where(array('user_id'=>$user_id,'status'=>2))->count();

        //已收货
        $can_up=D('orders_list')->where(array('user_id'=>$user_id,'status'=>3))->count();

        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),
            'data'=>array('none_pay'=>$none_pay,'can_pay'=>$can_pay,'can_get'=>$can_get,'can_up'=>$can_up)));
    }

    //@todo 获取全部订单
    public function getOrdersData(){
        $post=json_decode(file_get_contents("php://input"),true);
        $user_id=$post['user_id'];
        $status=$post['status'];
        $where=array(
            'user_id'=>$user_id
        );
        switch($status){
            case 0 :break;
            case 1 : $where['status']='0';break;
            case 2 : $where['status']='1';break;
            case 3 : $where['status']='2';break;
            case 4 : $where['status']='3';break;
            case 6 : $where['status']=array('IN',array('6','7'));break;
            default:break;
        }
        $list=D('orders_list')
            ->field("*,FROM_UNIXTIME(ac_time) as ac_time,wx_orders_list.id as id")
            ->join("wx_orders_goods_list as gl on gl.orders_id=wx_orders_list.id")
            ->where($where)->group('wx_orders_list.id')->select();
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>$list));
    }

    //@todo 获取一条订单信息
    public function getOrdersInfo(){
        $post=json_decode(file_get_contents("php://input"),true);
        $id=$post['id'];
        $info=D('orders_list')->field("*,FROM_UNIXTIME(wx_orders_list.ac_time) as ac_time,wx_orders_list.id as id")
            ->join("wx_client_user_address as cud on cud.user_id =wx_orders_list.user_id")
            ->where(array('wx_orders_list.id'=>$id,'is_code'=>'1'))->find();
        if($info['xk_id']!= ''){
            $xk_info=D('photo_box')->where(array('id'=>$info['xk_id']))->find();
            $info['xk_code']=$xk_info['text'];
        }
        if($info['xc_id']!= ''){
            $xk_info=D('photo_box')->where(array('id'=>$info['xc_id']))->find();
            $info['xc_code']=$xk_info['text'];
        }
        $goods=D('orders_goods_list')->where(array('orders_id'=>$info['id']))->select();
        foreach($goods as $key=>$value){
            $goods[$key]['photo_url']="https://".$_SERVER['SERVER_NAME'].'/Uploads/Cms/UserPhoto/'.$value['photo_url'];
            if($value['product_id'] != 0){
                $product_info=D('product_pic')->where(array('id'=>$value['product_id']))->find();
                $goods[$key]['p_thumb']="https://".$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$product_info['thumb'];
            }
            if($value['decorate_id'] != 0){
                $decorate_info=D('product_next_pic')->where(array('id'=>$value['decorate_id']))->find();
                $goods[$key]['d_thumb']="https://".$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$decorate_info['thumb'];
            }
        }
        $info['goods']=$goods;
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'info'=>$info));
    }

    //@todo 取消一条订单
    public function removeOrders(){
        $post=json_decode(file_get_contents("php://input"),true);
        $id=$post['id'];
        $remove=D('orders_list')->save(array('id'=>$id,'status'=>5));
        if(!is_numeric($remove)){
            $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'服务器发生错误')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功')));
    }

    //@todo 确定收货
    public function confirmTake(){
        $post=json_decode(file_get_contents("php://input"),true);
        $id=$post['id'];
        $confirm=D('orders_list')->save(array('id'=>$id,'status'=>3));
        if(!is_numeric($confirm)){
            $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'服务器发生错误')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功')));
    }
    //@todo 付款成功操作
    public function editOrderStatus(){
        $post=json_decode(file_get_contents("php://input"),true);
        $id=$post['id'];
        $status=$post['status'];
        $confirm=D('orders_list')->save(array('id'=>$id,'status'=>$status));
        if(!is_numeric($confirm)){
            $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'服务器发生错误')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功')));
    }

    //@todo  查找有未处理的图片库
    public function  getPleaseOrder(){
        $post=json_decode(file_get_contents("php://input"),true);
        $user_id=$post['user_id'];
        $result=D('create_order')->where(array('user_id'=>$user_id))->select();
        if(!$result){
            $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>false));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>true));
    }
    //@todo  申请退款
    public function applyRefund(){
        $post=json_decode(file_get_contents("php://input"),true);
        $id=$post['id'];
        $applyRefund=D('orders_list')->save(array('id'=>$id,'status'=>'6'));
        if(!is_numeric($applyRefund)){
            $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功','data'=>false)));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>true));
    }
}